<template>
	<view class="content">
		 <view>
			  <view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/4
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<!-- !!!!! placeholder 在ios表现有偏移 建议使用 第一种样式 -->
			<view class="cu-form-group margin-top">
				<textarea maxlength="-1" :disabled="modalName!=null" @input="textareaAInput" placeholder="请描述您的问题"></textarea>
			</view>
		
		  <view class="box margin-top-20" style=" padding: 20upx;">
			 
				<button @click="postForm" class="cu-btn block bg-blue margin-tb-sm lg">
				<text :class="loadding==1?'cuIcon-loading2 cuIconfont-spin':'cuIconfont-spin'"></text> 提交</button>
			 
			</view>
			
		 </view>
	</view>
</template>

<script>
	import beeApp from '../../beeApp.js'
	
	export default {
		data() {
			return {
				modalName: null,
				imgList: [],
				loadding:0,
				pics:[],
				content:'',
			}
		},
		onLoad() {
          // beeApp.alert("yiqwyei");
		},
		methods: {
		ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						 
						for(let i in res.tempFilePaths ){
							beeApp.upload(res.tempFilePaths[i],(uploadRes)=>{
								this.imgList = this.imgList.concat(res.tempFilePaths[i])
								this.pics.push(uploadRes.key);
							})
						}
						
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这张图片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			textareaAInput(e) {
				this.content = e.detail.value
			},
        postForm:function(){
		  var that = this;
		  if(that.loadding==1){
			  return;
		  }
		 
		  if(this.content==""){
			  beeApp.alert("请描述您的问题");
			  return;
		  }
		   that.loadding =1;
          beeApp.api("sns/topic/add",{
			  pid:94,
			  title:this.content,
			  pics:this.pics.join(),
			  method:"POST",
		  },function(res){
			  that.loadding =0;
             if(res.code!=200){
                beeApp.alert(res.msg);
                return ;
             }
			beeApp.alert("提交成功，感谢你的建议",function(){
				beeApp.setPageData("pages/user/topics",{reload:1});
				uni.navigateBack();
				
			})
          });
        },
	}
		
}
</script>
